package com.edu.juc.bQueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class BlockQueueTest {
    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);

        // 第一组： add remove 抛出异常
        System.out.println(blockingQueue.add("aa"));
        System.out.println(blockingQueue.add("bb"));
        System.out.println(blockingQueue.add("cc"));
        System.out.println(blockingQueue.element());
        System.out.println(blockingQueue.element());
        System.out.println("blockingQueue.remove() = " + blockingQueue.remove());
        System.out.println(blockingQueue.remove("cc"));
        System.out.println("blockingQueue.remove() = " + blockingQueue.remove());
        System.out.println(blockingQueue.add("dd"));
        System.out.println("blockingQueue.remove() = " + blockingQueue.remove());

        // 第二组 offer 和 poll() 返回true或false
        System.out.println(blockingQueue.offer("ee"));
        System.out.println(blockingQueue.offer("ff"));
        System.out.println(blockingQueue.offer("gg"));
        System.out.println(blockingQueue.offer("hh"));
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());


        // 第三组： put 和 take 阻塞
        blockingQueue.put("xx");
        blockingQueue.put("yy");
        blockingQueue.put("zz");
//        blockingQueue.put("ww");
        System.out.println("blockingQueue.take() = " + blockingQueue.take());
        System.out.println("blockingQueue.take() = " + blockingQueue.take());
        System.out.println("blockingQueue.take() = " + blockingQueue.take());
//        System.out.println("blockingQueue.take() = " + blockingQueue.take());

        // 第四组 offer poll 超时
        System.out.println(blockingQueue.offer("p"));
        System.out.println(blockingQueue.offer("q"));
        System.out.println(blockingQueue.offer("r"));
        System.out.println(blockingQueue.offer("s", 3L, TimeUnit.SECONDS));
    }
}
